package com.pjl.restartlife.homepage.service.serviceImpl;

import com.pjl.restartlife.homepage.service.WeatherService;
import com.pjl.restartlife.homepage.vo.WeatherVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author pjl
 * @create 2024/04/2024/4/26
 * @description
 */
@Log4j2
@Service
public class WeatherServiceImpl implements WeatherService {
    public static final String WEATHER_URL = "http://t.weather.sojson.com/api/weather/city/{1}";
    @Override
    public WeatherVo getWeather(String city) {
       WeatherVo weatherVo = new WeatherVo();
       try {
           RestTemplate restTemplate=new RestTemplate();
           weatherVo = restTemplate.getForObject(WEATHER_URL, WeatherVo.class,city);
           if (weatherVo != null && weatherVo.isSuccess()) {
               return weatherVo;
           }else{
               log.error("获取天气数据返回错误：{}", weatherVo);
           }
       }catch (RuntimeException e){
           e.printStackTrace();
       }

        return weatherVo;
    }
}
